package com.example.verifysdk.scancode;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.example.verifysdk.BaseJson;
import com.example.verifysdk.Contents;
import com.example.verifysdk.LogUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 *
 * @author LiuV
 * @date 2017/6/14
 */

public class ShowQRInfoPresenter {

    private static final String TAG = "ShowQRInfoPresenter";

    private ShowQRInfoView mShowQRInfoView;
    private ShowQRInfoModel mShowQRInfoModel;
    private Gson gson = new Gson();

    public void setmShowQRInfoView(ShowQRInfoView mShowQRInfoView) {
        this.mShowQRInfoView = mShowQRInfoView;
    }

    public void loginScanUser(String pid, String uid) {
        LogUtil.i(TAG, "---loginScanUser===");
        if (mShowQRInfoModel == null) {
            mShowQRInfoModel = new ShowQRInfoModel();
        }
        mShowQRInfoModel.loginScanUser(mShowQRInfoView.getViewContext(), pid, uid, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                mShowQRInfoView.loginFail(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                if (res.contains(Contents.ERROR_TAG_1) || !res.contains(Contents.ERROR_TAG_2)) {

                    this.onFailure(call, new IOException("服务器响应异常"));
                    return;
                }
                BaseJson baseJson = gson.fromJson(res, BaseJson.class);
                if (baseJson == null || baseJson.getCode() != 0) {
                    mShowQRInfoView.loginFail(
                            new Exception(baseJson == null
                                    ? "Unknow Errow"
                                    : baseJson.getError()));
                } else {
                    JsonObject object = baseJson.getObj().getAsJsonObject();
                    String bean = gson.fromJson(object.get("token"), String.class);
                    mShowQRInfoView.loginSuccess(bean);
                }
            }
        });
    }

    public void checkSecret(String token, String sec) {
        if (mShowQRInfoModel == null) {
            mShowQRInfoModel = new ShowQRInfoModel();
        }
        mShowQRInfoModel.checkSecret(mShowQRInfoView.getViewContext(), token, sec, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                mShowQRInfoView.checkFail(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                if (res.contains(Contents.ERROR_TAG_1) || !res.contains(Contents.ERROR_TAG_2)) {
                    this.onFailure(call, new IOException("二维码不合法"));
                    return;
                }
                BaseJson baseJson = gson.fromJson(res, BaseJson.class);
                if (baseJson == null) {
                    mShowQRInfoView.checkFail(new Exception("Unknow Error"));
                    return;
                }
                if (baseJson.getCode() == 0){
                    if (baseJson.getObj() == null){
                        mShowQRInfoView.checkFail(new Exception("验证异常，请联系服务提供商"));
                        return;
                    }
                    String name = "";
                    String id = "";
                    boolean appShow = false;
                    if (!baseJson.getObj().isJsonNull()) {
                        JsonObject object = gson.fromJson(baseJson.getObj(), JsonObject.class);
                        if (object.toString().contains("name")){
                            name = object.get("name").getAsString();
                        }
                        if (object.toString().contains("id")){
                            id = object.get("id").getAsString();
                        }
                        if (object.toString().contains("appShow")){
                            appShow = object.get("appShow").getAsBoolean();
                        }
                        Log.i(TAG, "---checkSecret===" + id);
                    }
                    mShowQRInfoView.checkSuccess(processInfo(!appShow, name, id));
                    mShowQRInfoView.sendId(id);
                }else if (baseJson.getCode() == 99){
                    mShowQRInfoView.mustRelogin();
                }else {
                    mShowQRInfoView.checkFail(new Exception(baseJson.getError()));
                }
            }
        });
    }

    private String processInfo(boolean appShow, String name, String id) {
        String n = name.trim();
        String i = id.trim();
        if (appShow){
            return "姓名：" + n + "\n身份证：" + i;
        }else{
            if (n.length() == 2) {
                n = "*" + n.charAt(1);
            } else if (n.length() >= 3) {
                n = n.charAt(0) + "*" + n.charAt(n.length() - 1);
            }
            n = "姓名：" + n;
            if (!TextUtils.isEmpty(i) && i.length() == 18){
                i = "身份证：" + i.substring(0, 9) + "******" + i.substring(15, 18);
                return n + "\n" + i;
            }else {
                return n;
            }
        }
    }

    interface ShowQRInfoView {
        void loginSuccess(String token);

        void loginFail(Exception e);

        void checkSuccess(String info);

        void checkFail(Exception e);

        void mustRelogin();

        void sendId(String id);

        Context getViewContext();
    }

}
